package com.jswlw.controller.api.helper;

import com.jswlw.common.wx.WeiXinUserInfo;
import com.jswlw.entity.base.User;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.service.base.UserService;
import com.jswlw.service.biz.DistributorService;
import com.jswlw.web.manager.SessionApiManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Component
public class UserHelper {

    @Autowired
    UserService userService;

    @Autowired
    DistributorService distributorService;

    public void createUser(WeiXinUserInfo weiXinUserInfo) {
        User user = userService.find4OpenId(weiXinUserInfo.getOpenid());
        if (Objects.isNull(user)) {
            user = new User();
            user.setOpenId(weiXinUserInfo.getOpenid());
            user.setPassword("123456");
            //user.setNickName(weiXinUserInfo.getNickname());
            //user.setWxName(weiXinUserInfo.getNickname());
            user.setWxPhoto(weiXinUserInfo.getHeadimgurl());
            userService.insert(user);
        }
    }



    public User findUser() {
        return userService.find4OpenId(SessionApiManager.getCurrentUser());
    }


    public Distributor findDistribtor(){
        User user=userService.find4OpenId(SessionApiManager.getCurrentUser());
        return distributorService.find4Id(Integer.valueOf(user.getLoginName()));
    }
}
